﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Threading;

namespace Shape
{
    
    public partial class Form1 : Form
    {
	static	string[] Source;
	static	string ResultPath;
		private OpenFileDialog fdialog;
		private SaveFileDialog folderdialog;
		private string folderName;
        public Form1()
        {
            InitializeComponent();
            this.MaximizeBox = false;
            this.KeyPreview = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
			fdialog = new OpenFileDialog();
            fdialog.Multiselect = true;//允许同时选择多个文件 
            fdialog.InitialDirectory = "c:\\";
            fdialog.Filter = "Shape files(*.shp)|*.shp|All files(*.*)|*.*|folders|*.neverseenthisfile";
			fdialog.CheckPathExists = false;
			fdialog.CheckFileExists = false;
            fdialog.FilterIndex = 1;
            fdialog.RestoreDirectory = true;
			fdialog.Title = "SHAPE文件选择框";


            if (fdialog.ShowDialog() == DialogResult.OK)
            {//写遍历函数

                if (fdialog.FileNames.Length==0)
                {
					MessageBox.Show("没有选择任何文件");
                }
				else
				{
				Source=Filesys.getfiles(fdialog.FileNames);
				comboBox1.DataSource = Source;
				}
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
			folderdialog = new SaveFileDialog();
			folderdialog.CheckPathExists = true;
			folderdialog.Filter = "Shape files(*.shp)|*.shp|All files(*.*)|*.*";
			folderdialog.FilterIndex = 1;
			folderdialog.Title = "选择合并结果保存文件夹";
			folderdialog.ValidateNames = true;
			folderdialog.RestoreDirectory = true;
			folderdialog.InitialDirectory = "c:\\";
			folderdialog.DefaultExt = "shp";
			folderdialog.AutoUpgradeEnabled = true;
			DialogResult result = folderdialog.ShowDialog();
			if (result == DialogResult.OK)
			{
				folderName = folderdialog.FileName;
				comboBox2.Items.Add(folderName);
				comboBox2.Text = folderName;
			}

			ResultPath = folderName;

        }

		private void button3_Click(object sender, EventArgs e)

		{
			Thread mThread = new Thread(new ThreadStart(threadmerge));	
			mThread.Start();
			
		}

		public void threadmerge() {Shapelib.ShapeMerge(Source, ResultPath); }

		private void Form1_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				this.Close();
			}

		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			Source[0]=comboBox1.Text;
			
		}

		private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
		{
			ResultPath = comboBox2.Text;
		}

	}


}
